package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserSeriver;
import com.itheima.service.impl.UserSeriverImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserSeriver us = new UserSeriverImpl();
        List<User> users = us.findAll();

        request.setAttribute("users",users);

        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {


        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserSeriver us = new UserSeriverImpl();
        us.save(user);

        response.sendRedirect(request.getContextPath()+"/userServlet?method=list");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");

        UserSeriver us = new UserSeriverImpl();
        us.deleteById(id);

        response.sendRedirect(request.getContextPath()+"/userServlet?method=list");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        //2. 获取请求参数Map
        Map<String, String[]> map = request.getParameterMap();
        //3. 封装数据
        User user = new User();
        try {
            BeanUtils.populate(user, map);
            //4.调用service保存
            UserSeriver service = new UserSeriverImpl();
            service.update(user);
            //5.重定向再次查询
            response.sendRedirect(request.getContextPath() + "/userServlet?method=list");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException | IOException e) {
            e.printStackTrace();
        }
    }


    public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取修改id
        String id = request.getParameter("id");

        //2. 调用service查询
        UserSeriver service = new UserSeriverImpl();
        User user = service.findById(id);

        //3. 将user添加到请求域中, 转发到update.jsp 回显
        request.setAttribute("u",user);
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }

}
